/*
 * $Header: $ 
 */
package net.frankish.moviebank.back;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import net.frankish.moviebank.util.Configs;
import net.frankish.tools.*;

/**
 * 
 * <p>Title: $RCSfile: XMLController.java,v $</p>
 * File: XMLController.java
 * Project: JCharacter
 * @author Frankish
 * @version $Revision:  $
 */
public class XMLController {

	String filePath = null;
	long lastKey = 0;
	public XMLController(){
		filePath = Configs.getInstance().getProperty(Configs.DATA_FOLDER);
	}
	
	public void update(String filename, Object data)throws Exception{
		PersistentBean bean = (PersistentBean)data;
		String xml = XMLSerialization.object2XML(bean);
		File file = new File(filePath + bean.getKey()+".xml");
		if (file.exists() == false) {
			if (file.createNewFile() == false) {
				throw new Exception("Unable to create file '" + filePath + bean.getKey()+".xml" + "'");
			}
		}
		FileWriter writer = new FileWriter(file, false);
		writer.write(xml);
		writer.flush();
		writer.close();
	}
	
	public Object refresh(String filename)throws Exception{
		File file = new File(filePath + filename);
		if (file.exists() == false) {
			
			throw new Exception("Unable to open file '" + filePath + filename + "'");
			
		}
		return readFile(file);
	}
	
	public void remove(String filename){
		File file = new File(filePath + filename);
		if (file.exists() == true) 
			file.delete();
	}
	
	public void create(String filename, Object data) throws Exception{
		PersistentBean bean = (PersistentBean)data;
		bean.setKey(new Long(generateKey()));
		String xml = XMLSerialization.object2XML(bean);
		File file = new File(filePath + bean.getKey()+".xml");
		if (file.exists() == false) {
			if (file.createNewFile() == false) {
				throw new Exception("Unable to create file '" + filePath + bean.getKey()+".xml" + "'");
			}
		}
		FileWriter writer = new FileWriter(file, false);
		writer.write(xml);
		writer.flush();
		writer.close();
	}
	
	
	public Collection loadData(){
		ArrayList res = new ArrayList();
		File file = new File(filePath);
		File[] files = file.listFiles(new FilenameFilter(){
			public boolean accept(File dir, String name) {
				if(name.indexOf(".xml") > 0)
					return true;
				return false;
			}	
		});
		for(int i = 0; i < files.length; i++){
			res.add(readFile(files[i]));
		}
		return res;
	}
	
	private Object readFile(File file) {
		try {
			String buf = new String("");
			FileReader read = new FileReader(file);
			
			while(read.ready())
				buf = buf +(char)read.read();
			read.close();
			return XMLSerialization.xml2Object(buf);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}

	private long generateKey(){
		long key = System.currentTimeMillis();
		while(key == lastKey){
			key = System.currentTimeMillis();
		}
		lastKey = key;
		return key;
	}
}
